package com.java.basis.lambda;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * 6.与函数式接口Predicate配合
 *
 * 除了在语言层面支持函数式编程风格，Java 8也添加了一个包，叫做 java.util.function。它包含了很多类，
 * 用来支持Java的函数式编程。其中一个便是Predicate，使用 java.util.function.Predicate 函数式接口
 * 以及lambda表达式，可以向API方法添加逻辑，用更少的代码支持更多的动态行为。Predicate接口非常适用于做过滤。
 *
 * @author wangning
 * @create 2021-10-26 15:33
 */
public class LambdaTest02 {
	public static void filterTest(List<String> languages, Predicate<String> condition) {
		languages.stream().filter(x -> condition.test(x)).forEach(x -> System.out.println(x + " "));
	}

	public static void main(String[] args) {
		List<String> languages = Arrays.asList("Java","Python","scala","Shell","R");
		System.out.println("Language starts with J: ");
		filterTest(languages,x -> x.startsWith("J"));
		System.out.println("\nLanguage ends with a: ");
		filterTest(languages,x -> x.endsWith("a"));
		System.out.println("\nAll languages: ");
		filterTest(languages,x -> true);
		System.out.println("\nNo languages: ");
		filterTest(languages,x -> false);
		System.out.println("\nLanguage length bigger three: ");
		filterTest(languages,x -> x.length() > 4);
	}

}
